package com.webinsky.publish.persist.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.webinsky.common.proj.DelFlag;
import com.webinsky.common.utils.DateUtils;
import com.webinsky.publish.domain.InfoFilter;

public class InfoEntityProvider {
	
	private static String SELECT_BASE = "select id, name, telphone, email, content, company, address, remark, info_date, create_at, del, site_id from `info` where del = " + DelFlag.UNDel.getFlag();
	private static String SELECT_COUNT = "select count(*) from `info` where del = " + DelFlag.UNDel.getFlag();
	
	public String selectByFilter(Map<String, Object> params) {
		InfoFilter filter = (InfoFilter)params.get("filter");
		List<String> whereConditions = new ArrayList<>();
		if ( filter != null ) {
			if ( filter.getEmail() != null ) {
				whereConditions.add(" email like '%" + filter.getEmail() + "%'");
			}
			if ( filter.getMobile() != null ) {
				whereConditions.add(" telphone like '%" + filter.getMobile() + "%'");
			}
			if ( filter.getSiteId() > 0 ) {
				whereConditions.add(" site_id =" + filter.getSiteId());
			}
			if ( filter.getContent() != null ) {
				whereConditions.add(" content like '%" + filter.getContent() + "%'");
			}
			if ( filter.getName() != null ) {
				whereConditions.add(" name like '%" + filter.getName() + "%'");
			}
			if ( filter.getInfoDateStart() != null) {
				whereConditions.add(" info_date >= '" + DateUtils.format(filter.getInfoDateStart()) + "'" );
			}
			if (  filter.getInfoDateEnd() != null ) {
				whereConditions.add(" info_date <= '" + DateUtils.format(filter.getInfoDateEnd()) + "'" );
			}
		}
		String sql = SELECT_BASE;
		if ( !whereConditions.isEmpty() ) {
			sql = sql + " and " + String.join(" and ", whereConditions);
		}
		sql = sql + " order by info_date desc " + " limit " + filter.getStart() + "," + filter.getSize();
		return sql;
	}
	
	public String selectCountByFilter(Map<String, Object> params) {
		InfoFilter filter = (InfoFilter)params.get("filter");
		List<String> whereConditions = new ArrayList<>();
		if ( filter != null ) {
			if ( filter.getEmail() != null ) {
				whereConditions.add(" email like '%" + filter.getEmail() + "%'");
			}
			if ( filter.getMobile() != null ) {
				whereConditions.add(" telphone like '%" + filter.getMobile() + "%'");
			}
			if ( filter.getSiteId() > 0 ) {
				whereConditions.add(" site_id =" + filter.getSiteId());
			}
			if ( filter.getContent() != null ) {
				whereConditions.add(" content like '%" + filter.getContent() + "%'");
			}
			if ( filter.getName() != null ) {
				whereConditions.add(" name like '%" + filter.getName() + "%'");
			}
			if ( filter.getInfoDateStart() != null) {
				whereConditions.add(" info_date >= '" + DateUtils.format(filter.getInfoDateStart()) + "'" );
			}
			if (  filter.getInfoDateEnd() != null ) {
				whereConditions.add(" info_date <= '" + DateUtils.format(filter.getInfoDateEnd()) + "'" );
			}
		}
		String sql = SELECT_COUNT;
		if ( !whereConditions.isEmpty() ) {
			sql = sql + " and " + String.join(" and ", whereConditions);
		}
		return sql;
	}
}
